package com.yhy.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yhy.domain.*;
import com.yhy.domain.vo.PageResult;
import com.yhy.domain.vo.SettingsVo;
import com.yhy.dubbo.api.BlackListApi;
import com.yhy.dubbo.api.QuestionApi;
import com.yhy.dubbo.api.SettingsApi;
import com.yhy.server.interceptor.UserHolder;


import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service
public class SettingsService {

    @Reference
    private SettingsApi settingsApi;

    @Reference
    private QuestionApi questionApi;

    @Reference
    private BlackListApi blackListApi;


    public ResponseEntity settings(){
        SettingsVo settingsVo = new SettingsVo();
//        封装settingsvo
        User user = UserHolder.getUser();
//        1.手机号phone
        settingsVo.setPhone(user.getMobile());

//        2.陌生人问题 strangerQuestion
        Question question = questionApi.findQuestionByUid(user.getId());

        settingsVo.setStrangerQuestion(question==null?"你喜欢java吗?":question.getTxt());

//        3.当前用户的通知配置
        Settings settings = settingsApi.findAdviceByUid(user.getId());
        if(settings != null){
            BeanUtils.copyProperties(settings,settingsVo);
        }
        return ResponseEntity.ok(settingsVo);
    }

    public ResponseEntity questions(Map map) {
        String content = (String) map.get("content");

        Long userId = UserHolder.getUserId();
        Question question = questionApi.findQuestionByUid(userId);
        if(question != null){
            question.setTxt(content);
            questionApi.update(question);
        }else{
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionApi.save(question);
        }
        return ResponseEntity.ok(null);


    }

    public ResponseEntity notifications(Settings settings) {

        Long userId = UserHolder.getUserId();

        Settings setting = settingsApi.findAdviceByUid(userId);

        if(setting != null){
            setting.setGonggaoNotification(settings.getGonggaoNotification());
            setting.setLikeNotification(settings.getLikeNotification());
            setting.setPinglunNotification(settings.getPinglunNotification());
            settingsApi.update(setting);
        }else{
            settings.setUserId(userId);
            settingsApi.save(settings);
        }
        return ResponseEntity.ok(null);
    }

    public ResponseEntity blacklist(Integer page, Integer pagesize) {
        Long userId = UserHolder.getUserId();

        IPage<UserInfo> iPage = blackListApi.findPage(page,pagesize,userId);

        PageResult pageResult = new PageResult(page,pagesize, (int) iPage.getTotal(),iPage.getRecords());

        return ResponseEntity.ok(pageResult);
    }

    public ResponseEntity deleteBlackList(Long blackUserId) {
        Long userId = UserHolder.getUserId();
        blackListApi.deleteBlackList(userId,blackUserId);
        return ResponseEntity.ok(null);
    }
}
